#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 0  // undirected graph
#define vertex_max 4
#define edge_max   5
#endif

#if 0  // directed graph
#define vertex_max 3
#define edge_max   4
#endif

#if 1
#define vertex_max 5
#define edge_max   (8*2)
#endif

struct graph
{
  int vertex[vertex_max + 1]; // 存放顶点信息，不使用vertex[0]
  int adjacency_matrix[vertex_max + 1][vertex_max + 1];  // 邻接矩阵
};

/* 无向图的邻接矩阵 */
void undirected_graph(struct graph * graph)
{
  int i, j, k;

  for (k = 1; k <= vertex_max; k++)
    scanf("%d", &graph->vertex[k]);
  
  for (i = 1; i <= vertex_max; i++)
    for (j = 1; j <= vertex_max; j++)
      graph->adjacency_matrix[i][j] = 0;

  for (k = 1; k <= edge_max; k++) {
    scanf("%d %d", &i, &j);
    printf("i=%d j=%d\n", i, j);
    graph->adjacency_matrix[i][j] = 1;
    graph->adjacency_matrix[j][i] = 1;
  }
}

/* 有向图的邻接矩阵 */
void directed_graph(struct graph * graph)
{
  int i, j, k;

  for (k = 1; k <= vertex_max; k++)
    scanf("%d", &graph->vertex[k]);
  
  for (i = 1; i <= vertex_max; i++)
    for (j = 1; j <= vertex_max; j++)
      graph->adjacency_matrix[i][j] = 0;
  
  printf("input edge:\n");
  for (k = 1; k <= edge_max; k++) {
    scanf("%d %d", &i, &j);
    graph->adjacency_matrix[i][j] = 1;
  }
}

/* 无向网的邻接矩阵 */
void undirected_quan_graph(struct graph * graph)
{
  int i, j, k;
  int w; // 权值

  for (k = 1; k <= vertex_max; k++)
    scanf("%d", &graph->vertex[k]);

  for (i = 1; i <= vertex_max; i++)
    for (j = 1; j <= vertex_max; j++) 
      if (i == j)
	graph->adjacency_matrix[i][j] = 0;
      else
	graph->adjacency_matrix[i][j] = 9999;
  
  for (k = 1; k <= edge_max; k++) {
    scanf("%d %d %d", &i, &j, &w);
    graph->adjacency_matrix[i][j] = w;
    graph->adjacency_matrix[j][i] = w;
  }
}

/* 有向图的邻接矩阵 */
void directed_quan_graph(struct graph * graph)
{
  int i, j, k;
  int w; // 权值

  for (k = 1; k <= vertex_max; k++)
    scanf("%d", &graph->vertex[k]);

  for (i = 1; i <= vertex_max; i++)
    for (j = 1; j <= vertex_max; j++) {
      if (i = j)
	graph->adjacency_matrix[i][j] = 0;
      else
	graph->adjacency_matrix[i][j] = 9999;
    }

  for (k = 1; k <= edge_max; k++) {
    scanf("%d %d %d", &i, &j, &w);
    graph->adjacency_matrix[i][j] = w;
  }
}

void display(struct graph * graph, int vexter)
{
  int i, j, count;

  count = 0;
  for (i = 1; i <= vertex_max; i++)
    for (j = 1; j <= vertex_max; j++) {
      printf(" %d ", graph->adjacency_matrix[i][j]);
      count++;
      if (count % vexter == 0) 
	printf("\n");
    }

  printf("\n");
}


int main()
{
  struct graph graph;

#if 0 // undirected graph
  /*
    1 2 3 4 : 顶点
    （1 2）（1 4）(2 4) (2 3) (3 4) :  :  连接关系
  */
  undirected_graph(& graph);
  display(& graph, 4);
#endif
#if 0
  directed_graph(& graph);
  display(& graph, 3);
#endif

#if 1
  undirected_quan_graph(& graph);
  display(& graph, 5);
#endif


}
